﻿Imports System.Windows.Forms
Imports System.IO

Public Class newWS

    Public pathToInstall As String = Nothing
    Public workspaceName As String = Nothing
    Public databaseName As String = Nothing
    Public databaseServer As String = Nothing
    Public dbConnexionStr As String = Nothing
    Friend parentMdi As Main
    Dim databaseUserName As String
    Dim databaseUserPwd As String

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        ' Me.DialogResult = System.Windows.Forms.DialogResult.OK
        If Not getPathInstall() Then
            Exit Sub
        End If
        If isFileNameValid() Then
            If dbConnexionStr Is Nothing Then
                MsgBox("Vous n'avez spécifié aucune base de donnée !!")
                Exit Sub
            End If
            workspaceName = wsName.Text
            Me.Close()
            Dim workspace As New Workspace()
            workspace.paramsHandler = Me
            workspace.MdiParent = parentMdi
            workspace.Text = wsName.Text
            workspace.WindowState = FormWindowState.Maximized
            workspace.mode = 1
            workspace.Show()
        Else
            MsgBox("Le nom de l'espace du travail n'est pas valide !!")
            Exit Sub
        End If

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Public Function isFileNameValid()
        Dim isValid As Boolean = True
        If wsName.Text.Trim() = String.Empty Then
            isValid = False
            Return False
        End If
        isValid = IsValidFileNameOrPath(wsName.Text)
        Return isValid
    End Function

    Public Function getPathInstall()

        If pathTool.SelectedPath <> String.Empty Then
            If Not Directory.Exists(pathTool.SelectedPath) Then
                MsgBox("Le repertoire que vous avez selectionné n'est pas valide !!")
                pathToInstall = String.Empty
                Return False
            End If

            pathToInstall = pathInstall.Text
            Return True
        Else
            MsgBox("Vous devez selectionner un repertoire valide !!")
            pathToInstall = String.Empty
            Return False
        End If
    End Function

    Private Sub Selectionner_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Selectionner.Click
        pathTool.ShowDialog(Me)
        pathInstall.Text = pathTool.SelectedPath
    End Sub

    Function IsValidFileNameOrPath(ByVal name As String) As Boolean
        ' Determines if the name is Nothing.
        If name Is Nothing Then
            Return False
        End If

        ' Determines if there are bad characters in the name.
        For Each badChar As Char In System.IO.Path.GetInvalidFileNameChars
            If InStr(name, badChar) > 0 Then
                Return False
            End If
        Next

        ' The name passes basic validation.
        Return True
    End Function

    Private Sub dbConBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dbConBtn.Click
        Dim dbConfig As DatabaseConfig = New DatabaseConfig()
        If dbConnexionStr IsNot Nothing And dbConnexionStr <> String.Empty Then
            dbConfig.conStr = dbConnexionStr
            dbConfig.txtDatabaseName.Text = databaseName
            dbConfig.txtServerName.Text = databaseServer
            dbConfig.txtUserName.Text = databaseUserName
            dbConfig.pbPassword.Text = databaseUserPwd
        End If
        dbConfig.ShowDialog(Me)
        If dbConfig.conStr IsNot Nothing Then
            dbConnexionStr = dbConfig.conStr
            databaseName = dbConfig.txtDatabaseName.Text
            databaseServer = dbConfig.txtServerName.Text
            dbCon.Text = databaseServer & "\" & databaseName
            databaseUserName = dbConfig.txtUserName.Text
            databaseUserPwd = dbConfig.pbPassword.Text
            dbConBtn.Text = "Modifier"
        Else
            dbConnexionStr = Nothing
            databaseName = Nothing
            databaseServer = Nothing
            dbCon.Text = ""
            dbConBtn.Text = "Connexion"
        End If
    End Sub
End Class
